package com.example.demo.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.ItemMapper;
import com.example.demo.dao.ItemTypeMapper;
import com.example.demo.dao.ShopItemMapper;
import com.example.demo.dao.SupplierMapper;
import com.example.demo.entity.Item;
import com.example.demo.entity.ItemType;
import com.example.demo.entity.ShopItem;
import com.example.demo.entity.Supplier;
import com.example.demo.exception.SPIPException;
import com.example.demo.util.ConverterUtil;
import com.example.demo.util.HttpClientUtil;
import com.example.demo.util.HttpClientUtil.Method;
import com.example.demo.vo.ItemBean;
import com.google.common.base.Objects;

@Service
public class ItemService {
	@Autowired
	ItemTypeMapper itemTypeMapper;
	@Autowired
	SupplierMapper supplierMapper;
	@Autowired
	ItemMapper itemMapper;
	@Autowired
	ShopItemMapper shopItemMapper;

	@Transactional
	public Item queryByApi(Item parm) throws SPIPException {
		String reString = HttpClientUtil.requestUrl(
				"http://www.mxnzp.com/api/barcode/goods/details?barcode=" + parm.getCode(), null, Method.GET, null,
				null);
		if (StringUtils.isBlank(reString))
			return parm;
		JSONObject jsonObject = JSON.parseObject(reString);
		if (!Objects.equal(1, jsonObject.getInteger("code")))
			throw new SPIPException(jsonObject.getString("msg"));
		JSONObject data = jsonObject.getJSONObject("data");
		String typeName = data.getString("brand");
		String supplierName = data.getString("supplier");
		Item item = new Item();
		item.setType(addType(typeName));
		item.setTypeName(typeName);
		item.setSupplierId(addSupplier(supplierName));
		item.setSupplierName(supplierName);
		item.setCode(data.getString("barcode"));
		item.setItemName(data.getString("goodsName"));
		Pattern p = Pattern.compile("[^0-9.]");
		Matcher m = p.matcher(data.getString("price"));
		item.setPrice(ConverterUtil.toFloat(m.replaceAll("").trim()));
		item.setStandard(data.getString("standard"));
		item.setOperator();
		itemMapper.insertSelective(item);
		return item;
	}

	/**
	 * 新增商品分类
	 * 
	 * @param typeName
	 * @return
	 * @throws SPIPException
	 */
	public Integer addType(String typeName) throws SPIPException {
		if (StringUtils.isBlank(typeName))
			return 1;// 商品
		ItemType parm = new ItemType();
		parm.setTypeName(typeName);
		ItemType itemType = itemTypeMapper.selectOne(parm);
		if (itemType == null) {
			itemType = new ItemType();
			itemType.setOperator();
			itemType.setTypeName(typeName);
			itemTypeMapper.insertSelective(itemType);
		}
		return itemType.getId();
	}

	/**
	 * 新增厂商
	 * 
	 * @param supplierName
	 * @return
	 * @throws SPIPException
	 */
	public Long addSupplier(String supplierName) throws SPIPException {
		if (StringUtils.isBlank(supplierName))
			return null;
		Supplier parm = new Supplier();
		parm.setSupplierName(supplierName);
		Supplier supplier = supplierMapper.selectOne(parm);
		if (supplier == null) {
			supplier = new Supplier();
			supplier.setOperator();
			supplier.setSupplierName(supplierName);
			supplierMapper.insertSelective(supplier);
			return supplier.getId();
		}
		return supplier.getId();
	}

	/**
	 * put 门店商品
	 * 
	 * @param itemBean
	 * @throws SPIPException
	 */
	@Transactional
	public void put(ItemBean itemBean) throws SPIPException {
		Item item = itemBean.getItem();
		ShopItem shopItem = itemBean.getShopItem();
		if (item.getId() == null) {
			itemMapper.insertSelective(item);
		} else {
			itemMapper.updateByPrimaryKey(item);
		}

		if (shopItem.getItemId() == null) {
			shopItem.setItemId(item.getId());
			shopItemMapper.insertSelective(shopItem);
		} else {
			shopItemMapper.updateByPrimaryKey(shopItem);
		}
	}

	/**
	 * edit门店商品
	 * 
	 * @param itemBean
	 * @throws SPIPException
	 */
	@Transactional
	public void edit(ItemBean itemBean) throws SPIPException {
		ShopItem shopItem = itemBean.getShopItem();
		if (shopItem == null || itemBean.getShopItem().getShopId() == null)
			throw new SPIPException("40002", "未选择门店");
		put(itemBean);
	}
}
